<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 添加分类 -->
      <el-row>
        <el-col
          ><el-button type="primary" @click="goodsAddClassifyBth"
            >添加分类</el-button
          ></el-col
        >
      </el-row>

      <!-- 树状表格 -->
      <tree-table
        :data="GoodsClassifyListData"
        :columns="columnsProps"
        show-index
        :selection-type="false"
        border
        :expand-type="false"
        class="tree-table"
        index-text="#"
      >
        <!-- 是否有效 -->
        <template slot="isEffective" scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false"></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <!-- 排序 -->
        <template slot="sort" scope="scope">
          <el-tag v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
          <el-tag
            v-else-if="scope.row.cat_level === 1"
            type="success"
            size="mini"
            >二级</el-tag
          >
          <el-tag
            v-else-if="scope.row.cat_level === 2"
            type="warning"
            size="mini"
            >三级</el-tag
          >
        </template>
        <!-- 操作 -->
        <template slot="operation">
          <el-button type="primary" icon="el-icon-edit" size="mini"
            >编辑</el-button
          >
          <el-button type="danger" icon="el-icon-delete" size="mini"
            >删除</el-button
          >
        </template>
      </tree-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="goodsClassfiySizeChange"
        @current-change="goodsClassfiyCurrentChange"
        :current-page="classifyInfoQuery.pagenum"
        :page-sizes="[3, 5, 10, 15, 100]"
        :page-size="classifyInfoQuery.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="goodsClassifyTotal"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="goodsAddClassifyDialogVisible"
      width="50%"
      @close="goodsAddClassifyDialogVisibleClose"
    >
      <el-form
        label-width="100px"
        :model="goodsClassifyForm"
        :rules="goodsClassifyFormRules"
        ref="goodsClassifyFormRef"
      >
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="goodsClassifyForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader
            v-model="goodsClassifyCascaderValue"
            :options="GoodsClassifyTwoListData"
            :props="cascaderProps"
            @change="goodsClassifyCascaderChange"
            change-on-select
            :clearable="true"
          >
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="goodsAddClassifyDialogVisible = false"
          >取 消</el-button
        >
        <el-button type="primary" @click="addClassify">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品分类请求参数
      classifyInfoQuery: {
        pagenum: 1,
        pagesize: 5
      },
      // 商品分类列表总数
      goodsClassifyTotal: 0,
      // 商品分类数据
      GoodsClassifyListData: [],
      // 表格各列的配置
      columnsProps: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isEffective'
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operation'
        }
      ],
      // 控制添加分类对话框显示与隐藏
      goodsAddClassifyDialogVisible: false,
      // 商品分类表单数据对象
      goodsClassifyForm: {
        cat_name: '',
        cat_level: 0,
        cat_pid: 0
      },
      // 商品分类表单验证规则
      goodsClassifyFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 0, max: 12, message: '长度在 0 到 12 个字符', trigger: 'blur' }
        ]
      },
      // 级联选择器配置选项
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 二级分类
      GoodsClassifyTwoListData: [],
      // 级联选择器双向数据绑定
      goodsClassifyCascaderValue: []
    }
  },
  created() {
    this.getGoodsClassifyList()
  },
  methods: {
    // 获取商品分类数据列表
    async getGoodsClassifyList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.classifyInfoQuery
      })
      // console.log(res)
      this.goodsClassifyTotal = res.data.total
      this.GoodsClassifyListData = res.data.result
      console.log(this.GoodsClassifyListData)
    },
    // 当每页条数发生改变触发
    goodsClassfiySizeChange(size) {
      this.classifyInfoQuery.pagesize = size
      // 重新刷新页面
      this.getGoodsClassifyList()
    },
    // 当前页数发生改变的时候触发
    goodsClassfiyCurrentChange(pageNnm) {
      this.classifyInfoQuery.pagenum = pageNnm
      // 重新刷新页面
      this.getGoodsClassifyList()
    },
    // 添加分类按钮
    async goodsAddClassifyBth() {
      // 获取二级分类
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      this.GoodsClassifyTwoListData = res.data
      // console.log(this.GoodsClassifyTwoListData)
      // 显示添加分类对话框
      this.goodsAddClassifyDialogVisible = true
    },
    // 当级联选择器发生改变的时候触发
    goodsClassifyCascaderChange() {
      if (this.goodsClassifyCascaderValue.length > 0) {
        this.goodsClassifyForm.cat_pid = this.goodsClassifyCascaderValue[
          this.goodsClassifyCascaderValue.length - 1
        ]
        this.goodsClassifyForm.cat_level = this.goodsClassifyCascaderValue.length
      } else {
        this.goodsClassifyForm.cat_pid = 0
        this.goodsClassifyForm.cat_level = 0
      }
    },
    // 关闭添加分类对话框回调
    goodsAddClassifyDialogVisibleClose() {
      // 重置表单
      this.$refs.goodsClassifyFormRef.resetFields()
      // 重置级联选择器
      this.goodsClassifyCascaderValue = []
    },
    // 添加分类
    addClassify() {
      // 预验证
      this.$refs.goodsClassifyFormRef.validate(async vdata => {
        if (!vdata) return false
        const { data: res } = await this.$http.post(
          'categories',
          this.goodsClassifyForm
        )
        // console.log(res)
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        // 关闭添加分类对话框
        this.goodsAddClassifyDialogVisible = false
        // 重新发送请求刷新页面
        this.getGoodsClassifyList()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.tree-table {
  margin: 15px 0;
}
.el-icon-success {
  color: #90ee90;
  font-size: 14px;
}
.el-icon-error {
  color: red;
  font-size: 14px;
}
.el-cascader {
  width: 100%;
}
</style>
